<?php
//会员管理
if(!defined('IN_T')){
   die('hacking attempt');
}

//添加、编辑
if($act=='detail'){
	$lid = intval($_REQUEST['lid']);
	$nearby = $Db->query("SELECT * FROM ".$Base->table('company_nearby')." WHERE id=$lid","Row");
	//$lid非法，强制为0
	if(empty($nearby)){
		$lid = 0;
	}
	//显示界面
	if(empty($_POST)){
	    //读取所有商户
        $sql = "select name,id from ".$Base->table('company')." where p_id=0";
        $companys = $Db->query($sql);
		$tp->assign('nearby',$nearby);
		$tp->assign('companys',$companys);
	}
	//处理数据
	else{
		$res['status'] = 0;
		$date = array(
			'id'=>$lid,
			'c_id'=>intval($_REQUEST['c_id']),
			'name'=>Common::sfilter($_POST['name']),
			'link'=>Common::sfilter($_POST['link']),
			'distance'=>number_format($_POST['distance'],1,'.',''),
			'position'=>Common::sfilter($_POST['position']),
			'sort'=>intval($_POST['sort'])
			);
		// var_dump($link);die;
		if(empty($date['name'])){
			$res['msg'] = '景区名称不能为空！';
		}
		else if($date['c_id'] <= 0){
            $res['msg'] = '请选择商户！';
        }
		else if(empty($date['link'])){
			$res['msg']='景区链接不能为空！';
		}
		else if($date['distance']<=0){
			$res['msg']='请填写景区距离';
		}
		else if(empty($date['position'])){
			$res['msg']='请选择景区坐标';
		}
		else if(empty($date['sort'])){
			$res['msg']='排序不能为空';
		}
		else{				
			// var_dump($link);die;
			$id = $Db->replace($Base->table('company_nearby'),$date);
			if($id){
				$res = array(
					'status'=>'1',
					'msg'=>'提交成功',
					'href'=>'/'.COMPANY_PATH.'/?m=nearby'
					);
			}
		}			
		//var_dump($res);die;
		echo $Json->encode($res);
		exit;
	}			
}
//删除
else if($act=="delete"){
	$id = intval($_POST['id']);
	$res['status'] = 0;
	//验证是否是非法操作
	$Db->execSql("delete from ".$Base->table('company_nearby')." where id=$id ");
	echo $Json->encode(array('status'=>1));
	exit;
}
//列表
else if($act=='index'){
	$size = 15;
	$page = intval($_REQUEST['page']);
	$page = $page<1 ? 1 : $page;
	$name = Common::sfilter($_REQUEST['name']);
	$res = nearbyList($name,$page,$size);
	$tp->assign('list',$res);
	$pages = Common::set_page($size,$page,$res['count']);
	foreach ($pages as $key => $value) {
		$pages[$key]['url'] = "/".COMPANY_PATH."/?m=nearby&page=".$value['num'].$res['spm'];
	}
	$tp->assign("page",$page);
	$tp->assign("pages",$pages);
}
$tp->assign('position','104.852528,26.592354');	//六盘水默认坐标
$tp->assign('act',$act);
$tp->assign('nav','附近景区');

function nearbyList($name,$page,$size){
	$spm = '';
	$sql = "SELECT nearby.*,com.name cname FROM ".$GLOBALS['Base']->table('company_nearby').'as nearby '.
        "left join ".$GLOBALS['Base']->table('company')." as com on com.id=nearby.c_id ".
        "WHERE 1=1 ";
	if($name){
		$sql .= " AND nearby.name LIKE '%$name%'";
		$spm .= "&name=$name";
        $GLOBALS['tp']->assign('name',$name);
	}
	// echo $sql;
	$rescount = count($GLOBALS['Db']->query($sql));
	$sql .= " ORDER BY sort  LIMIT ".($page-1)*$size.", ".$size."";

	$list = $GLOBALS['Db']->query($sql);
	return array('list'=>$list,'count'=>$rescount,'spm'=>$spm);
}

?>